// webview.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: '游戏',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    gameUrl:'',
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  onLoad(option){
    console.log("webview--:",option,getApp().globalData.gameUrl)
    //getApp().globalData.gameUrl是跳转前的游戏加入页面
    this.setData({
      gameUrl: getApp().globalData.gameUrl     
    })
  },
  // 小程序 JS
onWebViewMessage(e) {
  const sessionID = wx.getStorageSync('session_id');
  //人为管理sessionkey
  console.log("webview sessionkey:",sessionID)
  this.setWebViewCookie(sessionID);
},
onWebViewLoad(){
  this.onWebViewMessage()
},
setWebViewCookie: function (sessionID) {
  // WebView 页面通过 JavaScript 注入 sessionID 到 Cookie
  const jsCode = `
      document.cookie = "SESSION_ID=" + "${sessionID}" + "; path=/";
  `;  
  const webViewContext = this.selectComponent('#webView');
  if (webViewContext) {
    this.webViewContext.evaluateJavascript({
      code: jsCode
    });
  }  
},
  onShow(e) {    
  },
  bindViewTap() {
    // wx.navigateTo({
    //   url: '../logs/logs'
    // })
    let url = decodeURIComponent('http://10.254.58.108:3001/guandan/threeJSguandan/game_guandan_room.html?room_id=204&user_id=7916#GameRoomCotrol');
    // 跳转到外部链接
    wx.navigateTo({
      url: '/pages/webview/webview?url=' + url
    });
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
    debugger
    this.getUserProfile()
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
     this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
    this.pageRouter.navigateTo({
      url: '../logs/logs'
    })
  },
  onClick(e) {
    const nickName = e.detail.value
    // this.pageRouter.navigateTo({
    //   url: '../logs/logs'
    // })
    
    let url = decodeURIComponent(`http://10.254.58.108:3001/guandan/threeJSguandan/game_guandan_room.html?room_id=204&user_id=7919#GameRoomCotrol`);
    // 跳转到外部链接
    wx.navigateTo({
      url: '/pages/webview/webview?url=' + url
    });
  },
  onError(){
    console.log('ylp-eerrr')
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        debugger
        this.setData({
          userInfo: res.userInfo,
          "userInfo.avatarUrl": res.userInfo.avatarUrl,
          hasUserInfo: true
        })
      }
    })
  },
})
